package com.cookbook.cookbookservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cookbook.cookbookservice.common.base.MidPage;
import com.cookbook.cookbookservice.mapper.CaipuIndexInfoMapper;
import com.cookbook.cookbookservice.mapstructs.CaipuIndexInfoMapStructs;
import com.cookbook.cookbookservice.model.domain.CaipuIndexInfoDO;
import com.cookbook.cookbookservice.model.domain.CaipuItemInfoDO;
import com.cookbook.cookbookservice.model.dto.CaipuIndexInfoPageDTO;
import com.cookbook.cookbookservice.model.vo.CaipuIndexInfoAdminVO;
import com.cookbook.cookbookservice.model.vo.CaipuIndexInfoVO;
import com.cookbook.cookbookservice.model.vo.CaipuItemInfoVO;
import com.cookbook.cookbookservice.service.CaipuIndexInfoService;
import com.cookbook.cookbookservice.service.CaipuItemInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: cookbook-service
 * @ClassName CaipuIndexInfoServiceImpl
 * @description:
 * @author: MiaoChao
 * @create: 2024-10-23 17:19
 * @Version 1.0
 **/
@Service
public class CaipuIndexInfoServiceImpl extends ServiceImpl<CaipuIndexInfoMapper, CaipuIndexInfoDO> implements CaipuIndexInfoService {

    @Autowired
    private CaipuItemInfoService caipuItemInfoService;

    @Override
    public MidPage<CaipuIndexInfoAdminVO> getPageCaipuIndexInfo(CaipuIndexInfoPageDTO caipuIndexInfoPageDTO) {
        PageHelper.startPage(caipuIndexInfoPageDTO.getCurrent(), caipuIndexInfoPageDTO.getSize());
        List<CaipuIndexInfoDO> list = this.list();
        PageInfo<CaipuIndexInfoDO> pageInfo = new PageInfo<>(list);
        MidPage<CaipuIndexInfoAdminVO> caipuIndexInfoVOMidPage = new MidPage<>();
        List<CaipuIndexInfoAdminVO> adminVOList = CaipuIndexInfoMapStructs.gm.toAdminVOList(pageInfo.getList());
        List<Long> ids = adminVOList.stream().map(CaipuIndexInfoAdminVO::getId).collect(Collectors.toList());
        LambdaQueryWrapper<CaipuItemInfoDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(CaipuItemInfoDO::getId, ids);
        List<CaipuItemInfoDO> itemInfoDOS = caipuItemInfoService.list(queryWrapper);
        Map<Long, CaipuItemInfoDO> collect = itemInfoDOS.stream().collect(Collectors.toMap(CaipuItemInfoDO::getId, o -> o));
        adminVOList.forEach(caipuIndexInfoAdminVO -> {
            CaipuItemInfoDO caipuItemInfoDO = collect.get(caipuIndexInfoAdminVO.getId());
            if (caipuItemInfoDO != null) {
                caipuIndexInfoAdminVO.setCaipuItemInfoVO(CaipuIndexInfoMapStructs.gm.toItemVO(caipuItemInfoDO));
            }
        });
        caipuIndexInfoVOMidPage.setRecords(adminVOList);
        caipuIndexInfoVOMidPage.setTotal(pageInfo.getTotal());
        caipuIndexInfoVOMidPage.setSize(pageInfo.getSize());
        caipuIndexInfoVOMidPage.setCurrent(pageInfo.getPageNum());
        caipuIndexInfoVOMidPage.setPages(pageInfo.getPages());
        return caipuIndexInfoVOMidPage;
    }
}